package com.edu.base.rest;

import com.edu.base.common.ParamsUtil;
import com.edu.base.ums.bannerConfig.domain.TbBannerConfig;
import com.edu.base.ums.bannerConfig.service.ITbBannerConfigRyService;
import com.edu.base.ums.bannerPosition.domain.AdBannerPosition;
import com.edu.base.ums.bannerPosition.service.IAdBannerPositionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 *获得banner图接口 
 * @author lzs
 * @version 2021-04-08
 */
@Api(tags = "2、banner图相关接口")
@RestController
@RequestMapping("/api/banner")
public class AppBannerConfigController extends  BaseController {
	@Autowired
	private ITbBannerConfigRyService bannerConfigRyService;
  	
	@Autowired
    private HttpServletRequest request;
	
	@Autowired
	private  IAdBannerPositionService adBannerPositionService;


	@GetMapping(value = "/list")
	@ApiOperation(value = "获得banner列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "positionId", value = "位置ID（1：APP引导页，2：APP启动页，3：APP首页轮播",required = true),
	})
	public AjaxResult getBannerList(String positionId){
		TbBannerConfig tbBannerConfig = new TbBannerConfig();
		tbBannerConfig.setBannerPositionId(positionId);
		tbBannerConfig.setPublishStatus("1");
	    List<TbBannerConfig> list =	bannerConfigRyService.selectTbBannerConfigRyList(tbBannerConfig);
		return AjaxResult.success(list);
	}

	/**
	 * 获得轮播图列表 
	 * @param
	 * @return
	 */
	@GetMapping(value = "/findBannerList")
	@ApiIgnore
	@ApiOperation(value = "获得轮播图列表接口",response =TbBannerConfig.class )
    @ApiImplicitParams({
    	    @ApiImplicitParam(name = "bannerPositionId", value = "位置ID（1：APP首页轮播，2：视频，3：商城H5端轮播；4：跟读列表"
    	    		+ "5：习题列表，6：卡拉OK列表，7：视频列表，8：课件，9：绘本）",required = true), 
       }) 
	public AjaxResult findBannerList(HttpServletResponse res,String bannerPositionId) {
       
		List<String> list = ParamsUtil.listAllParams(request);
		if(!list.isEmpty()) {
			// 参数列中的parameter1、parameter2、parameter3等参数并未定义在参数列表中，为无效参数。
			list.remove("bannerPositionId"); 
 			if(list.size() > 10) {
				list = list.subList(0, 10);
			}
		}
		
 		// 检验请求参数合法性
  		res.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受， */
		res.setHeader("Access-Control-Allow-Headers", "Content-Type");
		res.setCharacterEncoding("UTF-8"); // 设置编码格式
		res.setContentType("text/html"); // 设置数据格式
		res.setHeader("Pragma", "No-cache");
		res.setHeader("Cache-Control", "no-cache");
		res.setDateHeader("Expires", 0);
  		if(StringUtils.isEmpty(bannerPositionId)){
  			 return AjaxResult.error("位置ID不能为空"); 
	    }
 
  		TbBannerConfig  bannerConfig = new TbBannerConfig();
  		bannerConfig.setBannerPositionId(bannerPositionId);
  		bannerConfig.setPublishStatus("1");
  		//startPage();
  		List<TbBannerConfig> bannerConfigList = bannerConfigRyService.selectTbBannerConfigRyList(bannerConfig);
  		bannerConfigList.forEach(v->{
  			AdBannerPosition adBannerPosition = new AdBannerPosition();
  			adBannerPosition.setParentPositionId(v.getId()); 
  			v.setAdBannerPositionList(adBannerPositionService.selectAdBannerPositionList(adBannerPosition));
  		}); 
		String msg = ""; 
		String param = String.join("、",list);
        if(StringUtils.isNotBlank(param)) {
        	msg += "参数列中的" + param +"等参数并未定义在参数列表中，为无效参数。";
        }
        if(StringUtils.isNotBlank(msg)) {
        	return AjaxResult.error(msg); 
        }
        
        return  AjaxResult.success(bannerConfigList);
	}

}
